﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceCS.LinuxFilePermission.Core
{
    public class FilePermissionEntity
    {
        public FilePermissionEntity()
        {

        }

        public FilePermissionEntity(LinuxFile file)
        {
            File = file;
        }
        /// <summary>
        /// 
        /// </summary>
        public LinuxFile File { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public FilePermission OwnerPermission { get; set; } = FilePermission.Read | FilePermission.Write;
        /// <summary>
        /// 
        /// </summary>
        public FilePermission GroupPermission { get; set; } = FilePermission.Read | FilePermission.Write;
        /// <summary>
        /// 
        /// </summary>
        public FilePermission OtherPermission { get; set; } = FilePermission.Read;

        public override string ToString()
        {
            var permPrefix = $"{File.Id} {File.Name} {File.Owner.Name} {File.Group.Name} ";
            if (File.IsDirectory)
            {
                permPrefix += "d";
            }
            else
            {
                permPrefix += "-";
            }
            var ownerPermStr = GetPermissionString(OwnerPermission);
            var groupPermStr = GetPermissionString(GroupPermission);
            var otherPermStr = GetPermissionString(OtherPermission);
            return $"{permPrefix}{ownerPermStr}{groupPermStr}{otherPermStr}";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string ToBinaryString()
        {
            var permPrefix = $"{File.Id} {File.Name} {File.Owner.Name} {File.Group.Name} ";
            if (File.IsDirectory)
            {
                permPrefix += "d";
            }
            else
            {
                permPrefix += "-";
            }
            var ownerPermStr = GetPermissionBinaryString(OwnerPermission);
            var groupPermStr = GetPermissionBinaryString(GroupPermission);
            var otherPermStr = GetPermissionBinaryString(OtherPermission);
            return $"{permPrefix}{ownerPermStr}{groupPermStr}{otherPermStr}";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        private string GetPermissionString(FilePermission permission)
        {
            var permStr = "";

            if (permission.HasFlag(FilePermission.Read))
            {
                permStr += "r";
            }
            else
            {
                permStr += "-";
            }

            if (permission.HasFlag(FilePermission.Write))
            {
                permStr += "w";
            }
            else
            {
                permStr += "-";
            }

            if (permission.HasFlag(FilePermission.Execute))
            {
                permStr += "x";
            }
            else
            {
                permStr += "-";
            }
            return permStr;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="permission"></param>
        /// <returns></returns>
        private string GetPermissionBinaryString(FilePermission permission)
        {
            var permStr = "";

            if (permission.HasFlag(FilePermission.Read))
            {
                permStr += "1";
            }
            else
            {
                permStr += "0";
            }

            if (permission.HasFlag(FilePermission.Write))
            {
                permStr += "1";
            }
            else
            {
                permStr += "0";
            }

            if (permission.HasFlag(FilePermission.Execute))
            {
                permStr += "1";
            }
            else
            {
                permStr += "0";
            }
            return permStr;
        }
    }
}
